package com.wen.controller;

import com.wen.model.UserModel;
import com.wen.uilts.MyUilts;


import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.PrimitiveIterator;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private int hitCount;

    @Override
    public void init(){
        // 重置点击计数器
        hitCount = 0;
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("utf-8");
        //访问人数增加

        hitCount++;
//        将访问人数存储到Session中
        req.getSession().setAttribute("count", hitCount);
        System.out.println("执行了后台的servlet业务逻辑处理。。。");
        // 获取表单数据
        /*
            要求：request.getParameter（）方法的参数值和form表单控件的name属性值 保持一致；

         */
        String strName = req.getParameter("userName");
        String strPsw = req.getParameter("userPsw");
        System.out.println("登录的用户名：" + strName);
        System.out.println("登录的密码：" + strPsw);
        // 跳转到业务逻辑处理成功页面
        // 创建业务逻辑处理对象
        //UserModel userModel = new UserModel();
        boolean canLogined = MyUilts.getNewInstance(UserModel.class).doLogin(strName, strPsw);
        if (canLogined) {
            // 登录成功
            //将用户名存储到session中
            req.getSession().setAttribute("uName", strName);
            // 转发
            req.getRequestDispatcher("WEB-INF/viewPages/loginedmain.jsp").forward(req, resp);
        } else {
            // 失败之后继续回到登录页面
            // 重定向
            resp.sendRedirect("index.jsp");
        }
    }
}
